<?php
/*
 * @version		2.2.0
 * @category	Plugin
 * @package		Joomla
 * @author		Original Author <serafinobilotta@hotmail.com>
 * @license		GNU/GPL, see http://www.gnu.org/licenses/gpl-2.0.html
 * @link		http://www.p2warticles.com/2011/06/scrolling-share-bar-joomla/
 * @since		2.0.0
 */



// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin');

class plgSystemScrollingShareBar extends JPlugin
{
	function plgSystemScrollingShareBar(&$subject, $config)
	{
		parent::__construct($subject, $config);
		$this->loadLanguage();
	}

	//funzione per escludere articoli, categorie, menu e home
	function isShowed(){
		$view=JRequest::getVar('view');
		$showinhome = $this->params->get('showinhome');
		$showinarticle = $this->params->get('showinarticle');
		$showincategories = $this->params->get('showincategories');
		$showinmenu = (array) $this->params->get('showinmenu');

		$articles = (array) $this->params->get('e_articles');
		$categories = (array) $this->params->get('e_categories');

		//get menu
		$app = JFactory::getApplication();
		$menu = $app->getMenu();
		$lang = JFactory::getLanguage();


		//true se la home va esclusa
		if((strcmp("featured", $view) == 0 && $showinhome == true) || ($menu->getActive() == $menu->getDefault($lang->getTag()) && $showinhome == true)) {
			return false;
		}
		if($showinmenu!=''){
			foreach($showinmenu as $menuitem){
				if ($menu->getActive()==$menu->getItem((int)$menuitem)){
					return false;
				}
			}
		}

		//show in article
		if($view == 'article' && $showinarticle==true){
			return false;
		}
		if($view == 'category' && $showincategories==true){
			return false;
		}
		if($view == 'article'){
			$article_id = (int) JRequest::getVar('id');
			$db = &JFactory::getDBO();
			$query = 'SELECT catid FROM #__content WHERE id =' . $article_id;
			$db->setQuery($query);
			$data = $db->loadObject();
			$catid = $data->catid;
			$tmpcat = array();
			foreach ($categories as $valuec) {
				$tmpcat[] = (int) $valuec;
			}
			$tmparticles = array();
			foreach ($articles as $value) {
				$tmparticles[] = (int) $value;
			}
			if (in_array($catid, $tmpcat)){
				return false;
			}
			if (in_array($article_id, $tmparticles)){
				return false;
			}
		}
		if($view == 'category'){
			$cat_id = (int) JRequest::getVar('id');
			$tmpcat = array();
			foreach ($categories as $valuec) {
				$tmpcat[] = (int) $valuec;
			}
			if (in_array($cat_id, $tmpcat)){
				return false;
			}
		}
		return true;
	}



	function getButtons()
	{

		$document = &JFactory::getDocument();
		$uri =& JURI::getInstance();
		$url = $uri->getScheme().'://'.$uri->getHost().$uri->getPath().'?'.$uri->getQuery();
		$title=$document->getTitle();
		$analytics = $this->params->get('analytics');

		$bottoni="";
		$showfields=$this->params->get('showfields');
		foreach ($showfields as $button)
		{
			$bottoni.="<div class=\"single_share_button\">";
			switch ($button)
			{

				//Facebook Like Button
				case "facebook_like" :
					$facebook_like_version=$this->params->get('facebook_like_version');
					if($facebook_like_version=='XFBML')
					{
						$bottoni.="<div id=\"fb-root\"></div><div id=\"fb-root\"></div><script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0];
								if (d.getElementById(id)) return;  js = d.createElement(s); js.id = id;  js.src = \"//connect.facebook.net/".$this->params->get('facebook_like_lang')."/all.js#xfbml=1&appId=".$this->params->get('facebook_like_appid')."\";  fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script>";
						if ($analytics){
							$bottoni.="<script type=\"text/javascript\">_ga.trackFacebook();</script>";
						}
						$bottoni.="<fb:like send=\"".$this->params->get('facebook_like_send')."\" layout=\"box_count\" width=\"55\" show_faces=\"false\" colorscheme=\"".$this->params->get('facebook_like_colorscheme')."\" font=\"".$this->params->get('facebook_like_font')."\"></fb:like>";
					}
					if($facebook_like_version=='XFBMLtag')
					{
						if ($analytics){
							$bottoni.="<script type=\"text/javascript\">_ga.trackFacebook();</script>";
						}
						$bottoni.="<fb:like send=\"".$this->params->get('facebook_like_send')."\" layout=\"box_count\" width=\"55\" show_faces=\"false\" colorscheme=\"".$this->params->get('facebook_like_colorscheme')."\" font=\"".$this->params->get('facebook_like_font')."\"></fb:like>";
					}
					if($facebook_like_version=='iframe')
					{
						$bottoni.="<iframe src=\"http://www.facebook.com/plugins/like.php?app_id=232931930056241&amp;href=".urlencode(htmlentities($url))."&amp;send=false&amp;layout=box_count&amp;width=55&amp;show_faces=false&amp;action=like&amp;colorscheme=".$this->params->get('facebook_like_colorscheme')."&amp;font=".$this->params->get('facebook_like_font')."&amp;height=65\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:55px; height:65px;\" allowTransparency=\"true\"></iframe>";
					}
					if($facebook_like_version=='HTML5')
					{
						$bottoni.="<div id=\"fb-root\"></div><script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0];
								if (d.getElementById(id)) return;  js = d.createElement(s); js.id = id;  js.src = \"//connect.facebook.net/".$this->params->get('facebook_like_lang')."/all.js#xfbml=1&appId=".$this->params->get('facebook_like_appid')."\";  fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script>";
						if ($analytics){
							$bottoni.="<script type=\"text/javascript\">_ga.trackFacebook();</script>";
						}
						$bottoni.="<div class=\"fb-like\" data-send=\"".$this->params->get('facebook_like_send')."\" data-layout=\"box_count\" data-width=\"55\" data-show-faces=\"false\" data-colorscheme=\"".$this->params->get('facebook_like_colorscheme')."\" data-font=\"".$this->params->get('facebook_like_font')."\"></div>";
					}
					if($facebook_like_version=='HTML5tag')
					{
						if ($analytics){
							$bottoni.="<script type=\"text/javascript\">_ga.trackFacebook();</script>";
						}
						$bottoni.="<div class=\"fb-like\" data-send=\"".$this->params->get('facebook_like_send')."\" data-layout=\"box_count\" data-width=\"55\" data-show-faces=\"false\" data-colorscheme=\"".$this->params->get('facebook_like_colorscheme')."\" data-font=\"".$this->params->get('facebook_like_font')."\"></div>";
					}
					break;

					//FbShare.me
				case "facebook_share" :
					$bottoni.="<script src=\"http://widgets.fbshare.me/files/fbshare.js\"></script>";
					break;

					//Twitter official
				case "twitter_counter" :
					if ($analytics){
						$document->addScriptDeclaration("//Load Twitter JS-API asynchronously
								(function(){
								var twitterWidgets = document.createElement('script');
								twitterWidgets.type = 'text/javascript';
								twitterWidgets.async = true;
								twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
								// Setup a callback to track once the script loads.
								twitterWidgets.onload = _ga.trackTwitter;
								document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
					})();");
					}
					$bottoni.="<a href=\"http://twitter.com/share\" class=\"twitter-share-button\" data-count=\"vertical\" data-via=\"".$this->params->get('twitter_counter_user')."\">Tweet</a>
							<script type=\"text/javascript\" src=\"http://platform.twitter.com/widgets.js\"></script>";
					break;

					// Google +1
				case "google_plusone" :
					$bottoni.="<g:plusone size=\"tall\"></g:plusone><script type=\"text/javascript\" src=\"http://apis.google.com/js/plusone.js\"></script>";
					break;

					//Mail Counter
				case "mail_counter" :
					$bottoni.="<iframe src=\"http://getmailcounter.com/mailcounter/?url=".urlencode(htmlentities($url))."&title=".urlencode(htmlentities($title))."\" height=\"64\" width=\"50\" frameborder=\"0\" scrolling=\"no\"></iframe>";
					break;

					//Reddit
				case "reddit_com" :
					$bottoni.="<script type=\"text/javascript\" src=\"http://www.reddit.com/static/button/button2.js\"></script>";
					break;

					//Digg.com
				case "digg_com" :
					$bottoni.="<script type=\"text/javascript\">
							(function() {
							var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
							s.type = 'text/javascript';
							s.async = true;
							s.src = 'http://widgets.digg.com/buttons.js';
							s1.parentNode.insertBefore(s, s1);
			})();
							</script>
							<a class=\"DiggThisButton DiggMedium\"></a>";
					break;

					//LinkedIn
				case "linked_in" :
					if ($analytics){
						$document->addScriptDeclaration("//LinkedIn Share Button tracking
								function LinkedInShare() {
								_gaq.push(['_trackSocial', 'LinkedIn', 'Share']);
					}");
					}
					$bottoni.="<script src=\"http://platform.linkedin.com/in.js\" type=\"text/javascript\"></script>
							<script type=\"IN/Share\" data-counter=\"top\"></script>";
					break;

					//TweetMeme
				case "tweetmeme" :
					$bottoni.="<script type=\"text/javascript\">
							tweetmeme_source = '".$this->params->get('twitter_counter_user')."';
									</script><script type=\"text/javascript\" src=\"http://tweetmeme.com/i/scripts/button.js\"></script>";
					break;

					//StumbleUpon
				case "stumbleupon" :
					$bottoni.="<script src=\"http://www.stumbleupon.com/hostedbadge.php?s=5\"></script>";
					break;

					//Custom Code
				case "custom_code" :
					$bottoni.=$this->params->get('custom_code_c');
					break;
			}
			$bottoni.="</div>\n";
		}

		if ($this->params->get('showPoweredBy'))
		{
			$bottoni.="<div class=\"single_share_button\"><div style=\"
					background: none repeat scroll 0 0 rgba(0, 0, 0, 0.32);
					border-radius: 1000px;
					font-family: Lucida Grande;
					font-size:10px;
					margin: 0 0px;
					padding: 2px 10px;
					text-shadow: 0 1px rgba(0, 0, 0, 0.8);
					\"><a href=\"http://www.p2warticles.com/2011/06/scrolling-share-bar-joomla/\" style=\"color: #999999;\" target=\"_blank\">Eddie84</a></div></div>";
		}

		$bottoni= '<!--Start Scrolling Share Bar Plugin http://www.p2warticles.com/2011/06/scrolling-share-bar-joomla/ -->
				<div id="scrolling_share_bar" class="scrolling_share_bar_starting">'.$bottoni.'</div>
						<!-- End Scrolling Share Bar Plugin - Joomla -->';


		return $bottoni;
	}




	public function onAfterDispatch()
	{
		$app = JFactory::getApplication();
		if ($app->isAdmin() && JRequest::getVar('option') || !$this->isShowed())
		{
			return true;
		}
		if( JRequest::getVar('norender') !='' || JRequest::getVar('tmpl') == 'component'  ) return;

		//load params
		$loadjquery = $this->params->get('loadjquery');
		$effecttype = $this->params->get('effecttype');
		$analytics  = $this->params->get('analytics');
		$startposition = $this->params->get('startposition');


		// get the document object.
		$doc = JFactory::getDocument();

		//load jquery
		if($loadjquery){
			$doc->addScript('http://code.jquery.com/jquery-latest.js');
		}
		if($analytics){
			$doc->addScript('http://analytics-api-samples.googlecode.com/svn/trunk/src/tracking/javascript/v5/social/ga_social_tracking.js');
		}

		//load css
		$doc->addStyleDeclaration('
				#scrolling_share_bar{
				background:none repeat scroll 0 0 #FFFFFF;
				border:1px solid #DDDDDD;
				float:left;
				margin-left:'.$this->params->get('margin_left').'px;
				margin-top:10px;
				z-index:9999;
				padding:5px;
				text-align:center;
	}
				.scrolling_share_bar_starting{
				position:'.$startposition.';
	}
				.scrolling_share_bar_scrolling{
				position:fixed;
				top:16px;
	}

				.single_share_button{
				margin: 0 auto;
				margin-bottom:5px;
	}
				'.$this->params->get('scrolling_share_bar_style'));

		if($effecttype == 'fixed')
		{
			$doc->addScriptDeclaration('
					jQuery.noConflict();
					jQuery(document).ready(function($){
					//put content div class, when scroll beyond this y, float it
					var $postShare = $(\'#scrolling_share_bar\');
					if($(\'#scrolling_share_bar\').parent().length > 0){
					var descripY = parseInt($(\'#scrolling_share_bar\').parent().offset().top) - 20;
					$(window).scroll(function () {
					var scrollY = $(window).scrollTop();
					var fixedShare = $postShare.css(\'position\') == \'fixed\';
					//make sure .post_share exists
					if($(\'#scrolling_share_bar\').length > 0){
					if ( scrollY > descripY && !fixedShare ) {
					$postShare.stop().addClass(\'scrolling_share_bar_scrolling\').removeClass(\'scrolling_share_bar_starting\');
		} else if ( scrollY < descripY && fixedShare ) {
					$postShare.addClass(\'scrolling_share_bar_starting\').removeClass(\'scrolling_share_bar_scrolling\');
		}
		}
		});
		}
		});');
		}
		if($effecttype == 'sticky')
		{
			$doc->addScript('plugins/system/scrollingsharebar/js/jquery.easing.1.3.js');
			$doc->addScript('plugins/system/scrollingsharebar/js/stickysidebar.jquery.min.js');
			$doc->addScriptDeclaration('
					jQuery.noConflict();

					jQuery(function () {
					jQuery("#scrolling_share_bar").stickySidebar({
					speed: '.$this->params->get('speed').',
					easing: "'.$this->params->get('easing').'"
		});
		});');
		}
		$doc->addScriptDeclaration("
				jQuery.noConflict();
				jQuery(document).ready(function($) {
				if($(window).width()>=(".$this->params->get('clientwidth').")-20){
				$('#scrolling_share_bar').show();
	}else{
				$('#scrolling_share_bar').hide()
	}
				$(window).resize(function() {
				if($(window).width()>= (".$this->params->get('clientwidth').")-20){
				$('#scrolling_share_bar').show()
	}else{
				$('#scrolling_share_bar').hide()
	}
	});
	});");
		if ($this->params->get('opacity')){
			$doc->addScriptDeclaration('
					jQuery(document).ready(function($) {
					var singlebutton = $(".single_share_button").css("opacity");
					$(".single_share_button").mouseover(

					function() {
					$(this).stop().animate({
					"opacity": "1"
		}, "fast");
		});
					$(".single_share_button").mouseout(

					function() {
					$(this).stop().animate({
					"opacity": singlebutton
		}, "fast");
		});
		});');
		}

		// get the buffer
		$buffer = $doc->getBuffer('component');

		// your string to insert
		$button_string = $this->getButtons();

		// insert the string
		$buffer = $button_string . $buffer;

		// reset the buffer
		$doc->setBuffer($buffer, 'component');
		 
		return true;
	}

}